<?php
class MyRegistration_Controller extends UserController {

	public function __construct() {
		parent::__construct();
		$this->activeMenuItem = UserMenu::$MY_REGISTRATIONS;
	}

	public function index() {
		return $this->all();
	}

	public function all() {
		$this->viewData['regList'] = $this->dao->findRegistrationsByPerson(
					$this->getAuthOrSubstituteUser('id'), $this->getL10nLanguageCode());

		$this->viewData['viRegList'] = $this->dao->findViRegistrationsByPerson($this->getAuthOrSubstituteUser('id'));
		$this->renderSubview('user/myRegistration/myRegistrationList');
	}

	public function display() {
		$id = $this->input->get('id');
		$type = $this->input->get('type');

		if (! valid::isId($id)) {
			$this->redirect('/user/myRegistration/all');
		}

		if ($type == 'vi') {
			$viRegistration = $this->dao->getById('vi_registration', $id);
			if (! $viRegistration) {
				$this->msg->error('error.recordNotFound');
				$this->redirect('/user/myRegistration/all');
			}
			$this->mergeViewData(registrationHelper::loadViDisplayData($viRegistration, $this->dao));
			$this->renderSubview('user/myRegistration/myViRegistrationDisplay');
		}
		else {
			$this->viewData['registration'] = $this->getSeminarService()->loadRegistrationDisplayData($id);
			$this->viewData['seminar'] = $this->viewData['registration']['seminar'];
			if (! $this->viewData['registration']) {
				$this->redirect('/user/myRegistration/all');
			}
			$this->renderSubview('/user/myRegistration/myRegistrationDisplay');
		}
	}

	protected function loadFormData() {
	}

	public function cancelViRegistration() {
		$viRegistrationId = $this->input->post('id');

		if (! valid::isId($viRegistrationId)) {
			$this->redirectToList('error.invalidId');
		}

		$viRegistration = $this->dao->getById('vi_registration', $viRegistrationId);
		$viSeminar = $this->dao->getById('vi_seminar', $viRegistration['vi_seminar_id']);
		if (! registrationUtils::canBeCancelled($viRegistration, $viSeminar)) {
			$this->redirect('/user/myRegistration/all');
		}
		$viRegistration['cancelled_at'] = time();
		$this->dao->update('vi_registration', $viRegistration);
		$this->msg->info('myRegistration.cancellationSuccess', $viSeminar['name']);
		
		// Send a confirmation email to the user.
		$person = $this->getAuthOrSubstituteUser();
		$emailLang = $person['preferred_language'];
		$body = text::getInLang($emailLang, 'myRegistration.cancellationEmail-body', $viSeminar['name']);
		$mailResult = email::sendInUtf8(
				$this->getAuthOrSubstituteUser('first_name') . ' ' . $this->getAuthOrSubstituteUser('last_name'),
				$this->getAuthOrSubstituteUser('email'),
				text::getInLang($emailLang, 'myRegistration.cancellationEmail-subject'),
				email::addHeadingAndClosing($body, $person, $emailLang));
		if (! $mailResult) {
			$this->msg->warning('viregWizard.mailFailedRegistrationValid');
		}

		$this->redirect('/user/myRegistration/all');
	}
	
	public function cancelRegistration() {
		$registrationId = $this->input->post('id');
		$reason = $this->input->post('cancellation_reason');
		
		if (! valid::isId($registrationId)) {
			$this->redirectToList('error.invalidId');
		}
		if ($registration['person_id'] != $this->getAuthOrSubstituteUser('id') ) {
			// User may cancel only his own registrations
			$this->redirectToList('error.invalidId');
		}
		
		$this->getSeminarService()->cancelRegistration($registrationId, $reason);
		$this->redirect('/user/myRegistration/all');
	}
}
